﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ChBusiness.Wsbs;
using ChBusiness.FlowOperation;
using ExtBusiness.Menu;
using System.ComponentModel;
using ChBusiness;

public partial class Wsbs_ShenBao : System.Web.UI.Page
{
    /// <summary>
    /// 页面加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["LoginUserName"] == null)
        {
            Response.Redirect("~/" + ChBusiness.SystemConfig.LoginPage);
        }
        if (string.IsNullOrEmpty(hidIId.Value))//如果为空则获取iid
        {
            if (!string.IsNullOrEmpty(Request["iid"]) && Request["iid"] != "-1")
            {
                hidIId.Value = Request["iid"];
            }
            else//未获取到iid或者iid为-1时，重新生成一个iid
            {
                ChBusiness.FlowOperation.SerialInstanceOperation sioTemp = new ChBusiness.FlowOperation.SerialInstanceOperation();
                long decValue = sioTemp.GetIIDWithEnterprise("", "", "");
                hidIId.Value = decValue.ToString();
            }
        }

        if (!IsPostBack)
        {
            //获取当前办件的业务状态
            ShenBaoSubmit submit = new ShenBaoSubmit();
            hidPrintFlag.Value = submit.GetIsSubmit(hidIId.Value);

            string strZhuce = string.Empty;
            try
            {
                if (!string.IsNullOrEmpty(Session["LoginUserName"].ToString()))
                {
                    strZhuce = "欢迎：" + Session["LoginUserName"].ToString() + "  " + "<a href='javascript:LoginOut()'  class='top_exit'>退出<a>";
                }
                else
                {
                    strZhuce = " <a class='top-right-title' href='/isp/isp/projects/sbsm/bsdt/register.jsp'>注册</a>";
                }
            }
            catch
            {
                strZhuce = " <a class='top-right-title' href='/isp/isp/projects/sbsm/bsdt/register.jsp'>注册</a>";
            }
        }
        ViewState["UserName"] = Session["LoginUserName"].ToString();
    }

    

   

    /// <summary>
    /// 绑定TAB菜单按钮 
    /// </summary>
    protected void BindTitleMenu()
    {
        XtProcess cProcess = new XtProcess();
        string strFlowName =Request["flowname"];
        DataTable dtTable = cProcess.GetProcess(strFlowName);
    }

    /// <summary>
    /// 设置按钮样式
    /// </summary>
    /// <returns></returns>
    protected string GetSubmit()
    {
        string strIid = hidIId.Value;
        string strSaveBtnStyle = string.Empty;
        string strSubmitBtnStyle = string.Empty;
        string strPrintBtnStyle = string.Empty;
        string strWizardBtnStyle = string.Empty;

        if (!string.IsNullOrEmpty(strIid))
        {
            string strSubmit = hidPrintFlag.Value;
            string SerialFlowType = SystemConfig.InterfaceSerialName;//国家涉密基础测绘成果使用申报

            //0:未提交;1:已提交;2:已通过;3:预审;-1:返回补证;-2:驳回中止;-3:不予受理;-4:审批不通过
            //已经提交或者在内网受理的案件均不能再次保存提交
            if (strSubmit == "1" || strSubmit == "2" || strSubmit == "3" || strSubmit == "-2" || strSubmit == "-3" || strSubmit == "-4")
            {
                strSaveBtnStyle = "disabled='disabled'";
                strSubmitBtnStyle = "disabled='disabled'";
            }

            if (SerialFlowType != Request["flowname"])//国家涉密基础测绘成果使用申报业务默认打开打印功能
            {
                if (strSubmit == "0" || strSubmit == "1" || strSubmit == "-1" || strSubmit == "-3" || strSubmit == "-4" || strSubmit == "")//预审通过时,才能打印
                {
                    strPrintBtnStyle = "disabled='disabled'";
                }
            }
        }
        else
        {
            strSaveBtnStyle = "disabled='disabled'";
            strSubmitBtnStyle = "disabled='disabled'";
            strPrintBtnStyle = "disabled='disabled'";
            strWizardBtnStyle = "disabled='disabled'";
        }

        return string.Format(@"  <input id='btnAddCopy' type='button' value='复制添加'  onclick='CopyAndAdd()'  class='zxsbButton'/>&nbsp;<input id='saveform' type='button' value=' 保 存 ' {0} onclick='SaveInfo()'  class='zxsbButton'/>&nbsp;<input id='submitform' type='button' value=' 提 交 ' {1} onclick='SubmitInfo()' class='zxsbButton'/>&nbsp;<input id='printform' type='button' value=' 打 印 ' {2} onclick='framePrint()' class='zxsbButton' />&nbsp;<input id='printformView' type='button' value='打印预览' {2} onclick='framePrintPreview()' class='zxsbButton' />&nbsp;", strSaveBtnStyle, strSubmitBtnStyle, strPrintBtnStyle, strWizardBtnStyle);
    }

   
    /// <summary>
    /// 遍历各表生成tab选项卡
    /// </summary>
    /// <param name="row"></param>
    /// <returns></returns>
    protected string GetContent()
    {
        string getiniscript = string.Empty;

        XtProcess cProcess = new XtProcess();
        string strFlowName = Request["flowname"];
        DataTable dtTable = cProcess.GetProcess(strFlowName);

        string strHtml = string.Empty;

        string strServername = Request.Url.Authority;
        string strNewwebdisplay = ConfigurationManager.AppSettings["NewWebDisplay"];
        string strIId = hidIId.Value;
        string strTitle = string.Empty;
        string strFid = string.Empty;
        if (dtTable.Rows.Count > 0)
        {
            //默认添加权限打开参数--针对webservice2
            if (strNewwebdisplay.IndexOf("?") == -1)
            {
                strNewwebdisplay += "?readonly=false&input_index=0";
            }
            else
            {
                strNewwebdisplay += "&readonly=false&input_index=0";
            }

            for (int i = 0; i < dtTable.Rows.Count; i++)
            {
                strTitle = dtTable.Rows[i]["TABLEANOTHERNAME"].ToString();
                strFid = dtTable.Rows[i]["FID"].ToString();
                var userid = Session["UserID"].ToString();
                getiniscript = GetInitScript(strFlowName, strTitle, userid);
                strHtml += string.Format("<div  id='div{6}' title='{0}'  style='padding: 20px;' ><iframe  id='ifr{6}' title='{3}'  scrolling='no' frameborder='0' src=\"http://{1}{2}&fid={3}&userid={7}&iid={4}&initScript={5}\"  style='width:980px; height: 100%'   ></iframe></div>", strTitle, strServername, strNewwebdisplay, strFid, strIId, Server.UrlEncode(getiniscript), i, userid);
                if (i == 0)
                {
                    strHtml += string.Format("<script type='text/javascript'>strIframeSrc=window.frames['ifr0'];strIfrmetitle = '{0}';</script>", strTitle);
                }
            }

            string SerialFlowType = SystemConfig.InterfaceSerialName;//国家涉密基础测绘成果使用申报
            //一致性说明函打印
            string strSubmitFlag = hidPrintFlag.Value;
            if (strSubmitFlag == "3" || SerialFlowType == Request["flowname"])//国家涉密基础测绘成果使用申报业务默认打开打印功能。预算通过后状态才能打印
            {
                //说明函
                SerialInstanceOperation sioGloab = new SerialInstanceOperation();
                string strShuoMingFid = sioGloab.GetFirstFidByWname("材料保证书");
                //getiniscript = GetInitScript(Request["flowname"], "说明函", Session["UserID"].ToString());

                strHtml += string.Format("<div  id='div{5}' style='padding:20px;' title='说明函'  ><iframe   scrolling='no' id='ifr{5}' frameborder='0' src=\"http://{0}{1}&fid={2}&userid=-1&iid={3}&flowtype={4}\"  style='width:980px; height: 100%'></iframe></div>", strServername, strNewwebdisplay, strShuoMingFid, strIId, strFlowName, dtTable.Rows.Count);
            }

            if (SerialFlowType == Request["flowname"])//国家涉密基础测绘成果使用申报业务默认打开订单详情
            {
                strHtml += string.Format("<div  id='smInfo'  style='padding:5px;text-align:center' title='订单详情' > <iframe  scrolling='yes' id='ifrSmInfo' frameborder='0' src=\"{0}&iid={1}\"  style='width:980px; height: 100%'  ></iframe></div>", ConfigurationManager.AppSettings["SecretDwInfoUrl"], strIId);
            }

            string strCaiLiaoTitle = "查看材料";           
            //0:未提交;-1:返回补证;空 均可采用上传材料
            if (strSubmitFlag == "0" || strSubmitFlag == "-1" || string.IsNullOrEmpty(strSubmitFlag))
            {
                strCaiLiaoTitle = "上传材料";
            }

            strHtml += string.Format("<div  id='upCailiao'  style='padding:5px;text-align:center' title='{0}' > <iframe  scrolling='no' id='ifr{3}' frameborder='0' src=\"../Common/SbFileUploadNew.aspx?flowname={1}&NewsId={2}\"  style='width:980px; height: 100%'  ></iframe></div>", strCaiLiaoTitle, strFlowName, strIId, dtTable.Rows.Count+1);
            return strHtml;
        }

        return strHtml;
    }

    public string GetInitScript(string strFlowName, string strTabName, string strUserId)
    {
        ChBusiness.Wsbs.XtProcess cProcess = new ChBusiness.Wsbs.XtProcess();
        string strSqlData = string.Empty;//获取初始化设定
        string strTableName = string.Empty;//表名
        string strColName = string.Empty;//字段名
        string strDefaultValue = string.Empty;//初始值
        string strJSValue = string.Empty;

        //获取所有的默认表和字段
        string strWhere = string.Format(" and userid='{0}'", strUserId);//初始化设定条件
        System.Data.DataTable dtTable = cProcess.GetFlowDefault(strFlowName, strTabName);
        for (int i = 0; i < dtTable.Rows.Count; ++i)
        {
            strTableName += string.Format("'{0}',", dtTable.Rows[i]["target_tab_name"]);
            strColName += string.Format("'{0}',", dtTable.Rows[i]["target_tabcol_name"]);

            strSqlData = dtTable.Rows[i]["logic_sql"].ToString();
            System.Data.DataTable dtTemp = cProcess.GetDefaultValue(strSqlData + strWhere);
            if (dtTemp.Rows.Count > 0)
            {
                strDefaultValue += string.Format("{0},", dtTemp.Rows[0][0]);
            }
            else//为空也要加，否则对应不上
            {
                strDefaultValue += ",";
            }
        }

        if (!string.IsNullOrEmpty(strTableName) && strDefaultValue.Length > 1)
        {
            strTableName = strTableName.Substring(0, strTableName.Length - 1);
            strColName = strColName.Substring(0, strColName.Length - 1);
            strDefaultValue = strDefaultValue.Substring(0, strDefaultValue.Length - 1);

            strJSValue += string.Format("initdefaultvalue(\"{0}\",\"{1}\",\"{2}\");", strTableName, strColName, strDefaultValue);
        }

        return strJSValue;
    }
}
